{
 "metadata": {
  "name": "trace"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "from sympy import symbols\n",
      "from sympy.physics.quantum.trace import Tr\n",
      "from sympy.matrices.matrices import Matrix\n",
      "from IPython.core.display import display_pretty\n",
      "from sympy.printing.latex import *\n",
      "\n",
      "%load_ext sympyprinting"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "###Basic Examples"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "a, b, c, d = symbols('a b c d'); \n",
      "A, B = symbols('A B', commutative=False)\n",
      "t = Tr(A*B)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "t"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\mbox{Tr}\\left(A B\\right)$$"
       ],
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "Tr(A\u22c5B)"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "latex(t)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "\\mbox{Tr}\\left(A B\\right)"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "display_pretty(t)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "display_data",
       "text": [
        "Tr(\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5)))"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Using Matrices"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "t = Tr ( Matrix([ [2,3], [3,4] ]))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 15
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "t"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$6$$"
       ],
       "output_type": "pyout",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAAwAAAASCAYAAABvqT8MAAAABHNCSVQICAgIfAhkiAAAAO5JREFU\nKJHN0r1KQ0EQhuHnhAgBhaiIFpLOxs5O8CIsFG/A1spCL0CwsUtnaat4C7aWNooiCAEJKBb+oMGg\nSCzOHlyWlWDnV+3M7vvN7O7wRxWZXAs7+MIL3rCHXs5gGh0shXgS19isDtQSYB9tnIa4jgZec+5r\n+MD4sHtUOsTlsEP1aL2AeyxiGbNoYgs3KTiqfJVzbET5FTxhLgVmMEAfY1G+FqoepcBIAC4ybZ8p\n/6KoHOATd6F8ql5oeSIG4ARTGaCBLh7TjVW8V05BBZ5xkDECx9j1M2PruIpN0uFrYhvzofcH5ajc\n/lbhH+gb6f4rZTpaz0QAAAAASUVORK5CYII=\n",
       "prompt_number": 16,
       "text": [
        "6"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Example using modules in physics.quantum"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "from sympy.physics.quantum.density import Density\n",
      "from sympy.physics.quantum.spin import (\n",
      "    Jx, Jy, Jz, Jplus, Jminus, J2,\n",
      "    JxBra, JyBra, JzBra,\n",
      "    JxKet, JyKet, JzKet,\n",
      ")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "d = Density([JzKet(1,1),0.5],[JzKet(1,-1),0.5]); d"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\rho\\left(\\begin{pmatrix}{\\left|1,1\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}{\\left|1,-1\\right\\rangle }, & 0.5\\end{pmatrix}\\right)$$"
       ],
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5))"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "t = Tr(d)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "t"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\mbox{Tr}\\left(\\rho\\left(\\begin{pmatrix}{\\left|1,1\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}{\\left|1,-1\\right\\rangle }, & 0.5\\end{pmatrix}\\right)\\right)$$"
       ],
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "Tr(\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5)))"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "latex(t)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "\n",
        "\\mbox{Tr}\\left(\\rho\\left(\\begin{pmatrix}{\\left|1,1\\right\\rangle }, & 0.5\\end{p\n",
        "matrix},\\begin{pmatrix}{\\left|1,-1\\right\\rangle }, & 0.5\\end{pmatrix}\\right)\\r\n",
        "ight)"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "t.doit()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$1.0$$"
       ],
       "output_type": "pyout",
       "png": "iVBORw0KGgoAAAANSUhEUgAAABsAAAASCAYAAACq26WdAAAABHNCSVQICAgIfAhkiAAAASNJREFU\nOI3t1EErBGEcx/HPimLXgUiU5eKinGxyc8KLkLeDK+WqpChcpJQjBxc33JALsgfFwYrEOsws0za7\nBpOT3+U/z/eZZ77PMzPPwx8mU4P3Yg8DCZ/TjDlkUQzbqziuNyiHSZyinFAEy1iLtMdxja5aAwax\nhVkcfEPWjxdMVPErrCSdaVLZPB4Fry6aJdyioQIa/D4FXOKpip+jA0NpyrrxEMNLYe1JW1aK4RXW\nlqbsGW8xvCmsH31pyE5q8FxYb9KUHaM9hreGtZi2LB/DR3Av+FN/JcujJbw+EqxsNNLfiDEsCr5p\n3WwLNnXccVPAK3bDdgab2PA5+SnBduisJejCPs5CURl3OMR05L4+XGAmwrKCk2QdC9jB8Fcr+s+P\n8g572TfbrLZhHwAAAABJRU5ErkJggg==\n",
       "prompt_number": 12,
       "text": [
        "1.00000000000000"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 12
    }
   ],
   "metadata": {}
  }
 ]
}